package com.kang.controller;

import com.kang.pojo.*;
import com.kang.service.AddressService;
import com.kang.service.ItemsService;
import com.kang.service.OrderMainService;
import com.kang.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author 吃三明治派对中的康康
 * @Date 2022/5/4 18:28
 * @Version 1.0
 */
@Controller
@RequestMapping("/pay")
public class PayController {

    @Autowired
    ShoppingService shoppingService;
    @Autowired
    ItemsService itemsService;
    @Autowired
    AddressService addressService;
    @Autowired
    OrderMainService orderMainService;

    //支付购物车
    @RequestMapping("/list")
    public String payList(Model model, HttpServletRequest request){

        if(request.getSession().getAttribute("UserSession") == null) return "redirect:/toLogin";

        double pay = 0;
        List<OrderSub> orderSubs = shoppingService.searchAllOrder();
        ArrayList<Items> items = new ArrayList<>();
        //计算总价格
        for (OrderSub orderSub : orderSubs) {
            pay += orderSub.getSum();
            //查找商品信息加入到列表中
            items.add(itemsService.selectGoodsById(orderSub.getId()));
        }

        model.addAttribute("payModel",items);
        model.addAttribute("flag",1);
        model.addAttribute("payModelPay",pay);
        model.addAttribute("addressModel",addressService.researchAddressByUid(21));

        return "pay";
    }
    //支付一件商品
    @RequestMapping("/one/{gid}")
    public String payTest(Model model, @PathVariable long gid, HttpServletRequest request){

        if(request.getSession().getAttribute("UserSession") == null) return "redirect:/toLogin";

        model.addAttribute("onePayModel",itemsService.selectGoodsById(gid));
        model.addAttribute("addressModel",addressService.researchAddressByUid(21));

        return "pay";
    }

    //清空购物车
    @RequestMapping("/payResult/shopping")
    public String payResult(){

        double pay = 0;
        List<OrderSub> orderSubs = shoppingService.searchAllOrder();
        for (OrderSub orderSub : orderSubs) {
            pay += orderSub.getSum();
        }
        OrderMain orderMain = new OrderMain();
        orderMain.setRealname("张三");
        orderMain.setUid(1);
        orderMain.setOid(UUID.randomUUID().toString().replace("-",""));
        orderMain.setTotalprice(BigDecimal.valueOf(pay));
        orderMain.setIspay(1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        orderMain.setCreateTime(simpleDateFormat.format(new Date()));

        orderMainService.insertOrderMain(orderMain);

        shoppingService.deleteAllShopping();
        return "/payResult";
    }

    //付款单个物品
    @RequestMapping("/payResult")
    public String payResultOne(){
        OrderMain orderMain = new OrderMain();
        orderMain.setRealname("张三");
        orderMain.setUid(1);
        orderMain.setOid(UUID.randomUUID().toString().replace("-",""));
        orderMain.setTotalprice(BigDecimal.valueOf(1));
        orderMain.setIspay(1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        orderMain.setCreateTime(simpleDateFormat.format(new Date()));

        orderMainService.insertOrderMain(orderMain);
        return "/payResult";
    }
}
